<template>
  <el-upload
      accept="image/*"
      :action="'/api/img?authorId='+authorId"
      :show-file-list="false"
      :on-success="handleAvatarSuccess"
      :before-upload="beforeAvatarUpload"
  >
    <slot></slot>
  </el-upload>
</template>

<script>
export default {
  name: "ImgUpload",
  props:{
    authorId:{
      type: Number,
      required: true
    }
  },
  methods: {
    handleAvatarSuccess(res) {
      if(res.code===0){
        this.$emit("success",res.data);
      }else if(res.code>0){
        this.$message.error(res.msg);
      }
      
    },
    beforeAvatarUpload(file) {
      
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isLt2M) {
        this.$message.error('图片大小不能超过 2MB!')
      }
      return  isLt2M
    }
  },
  emits:["success"]
}
</script>

<style scoped>

</style>